<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* wish上传商品类
*/
class Wishuploadproduct
{
	
	function __construct()
	{
		$this->CI =& get_instance();
        $this->db = $this->CI->load->database('default', TRUE);
	}

   public function upload(){
   $sql1="select tp.* from db_wish_to_platform as tp,d_shop as sp where tp.is_upload='0' and tp.shop_id=sp.id and sp.agent_visit='0';";
   $arr=$this->db->query($sql1)->result_array();
   foreach ($arr as $value) {
   	$shopid=$value['shop_id'];
   	$productid=$value['product_id'];
     $sql2="select * from d_shop where id='{$shopid}'";
     $shopa=$this->db->query($sql2)->result_array();
     $tooken=json_decode($shopa[0]['amazonkey'],true);
     $access_token=$tooken[0]['access_token'];
     $wp_id=$value['wp_id'];
   	 //循环商品数组导入店铺

   	 $this->createproduct($access_token,$productid,$wp_id);

   	 
 
   }



   }


      //创建wish商品
       public function createproduct($access_token,$productid,$wp_id){
      global $link;

      $access_token = urlencode($access_token);

      $sql="select * from db_wishproduct where wish_id='{$productid}';";
      $query=$this->db->query($sql)->result_array();
       foreach ($query as $pv) {
          $name = urlencode(unserialize($pv['Product_Name']));
          $parent_sku = urlencode(unserialize($pv['Parent_Unique_ID']));
          $sku = urlencode(unserialize($pv['Unique_ID']));
          $inventory = urlencode(unserialize($pv['Quantity']));
          $price = urlencode(unserialize($pv['Price']));
          $shipping = urlencode(unserialize($pv['Shipping']));
          $description = urlencode(unserialize($pv['Description']));
          $tags = urlencode(unserialize($pv['Tags']));
          $main_image = urlencode(unserialize($pv['Main_Image_URL']));
          $extra_images = urlencode(unserialize($pv['Extra_Image_URL']));
          $color=urlencode(unserialize($pv['Color']));
          $size=urlencode(unserialize($pv['Size']));
          // $msrp=urlencode($pv['MSRP']);
          $shipping_time=urlencode(unserialize($pv['Shipping_Time']));
               

          $aurl = sprintf(
          "https://merchant.wish.com/api/v2/product?access_token=%s&parent_sku=%s",
          $access_token,$parent_sku);
$acontext = stream_context_create(array(
    'http' => array(
        'method'        => 'GET',
        'ignore_errors' => true,
    ),
));

// Send the request
$aresponse = file_get_contents($aurl, TRUE, $acontext);
$aresponsearr=json_decode($aresponse,true);
if ($aresponsearr['code']=='0') {
  //说明父级已经存在，添加产品变化
$url = sprintf(
    "https://merchant.wish.com/api/v2/variant/add?access_token=%s&sku=%s&inventory=%s&price=%s&size=%s&shipping=%s&parent_sku=%s&main_image=%s&shipping_time=%s&color=%s",
    $access_token, $sku, $inventory, $price, $size, $shipping, $parent_sku,$main_image,$shipping_time,$color);

$context = stream_context_create(array(
    'http' => array(
        'method'        => 'POST',
        'ignore_errors' => true,
    ),
));

// Send the request
$response = file_get_contents($url, TRUE, $context);

}else{
               $url = sprintf(
          "https://merchant.wish.com/api/v2/product/add?access_token=%s&main_image=%s&name=%s&description=%s&tags=%s&sku=%s&inventory=%s&price=%s&shipping=%s&extra_images=%s&parent_sku=%s&color=%s&size=%s&shipping_time=%s",
          $access_token, $main_image, $name, $description, $tags, $sku, $inventory, $price, $shipping, $extra_images, $parent_sku,$color,$size,$shipping_time);
          $context = stream_context_create(array(
                'http' => array(
                    'method'        => 'POST',
                    'ignore_errors' => true,
                ),
            ));
                  // Send the request
          $response = file_get_contents($url, TRUE, $context);
          
          

}

                   $responsearr=json_decode($response,true);
          $responseserialize=addslashes(serialize($responsearr));
          
                    if ($responsearr['code']=='0') {
                      //is_upload=1 代表同步到店铺成功
                       $sql2="UPDATE db_wish_to_platform set is_upload = 1,log='{$responseserialize}' where wp_id='{$wp_id}';";
                    }else{
                      //is_upload=1 代表同步到店铺失败
                       $sql2="UPDATE db_wish_to_platform set is_upload = 2,log='{$responseserialize}' where wp_id='{$wp_id}';";
                    }
                    
                    $this->db->query($sql2);








          
                   
                
            }




       }







}

?>